package String.s9;

import java.util.HashMap;
import java.util.Map;

public class test443 {
    public static void main(String[] args) {
      char[] chars = {'a','a','b','b','c','c','c','c','c','c','c','c','c','c','c','c',
              'c','c','c','c','c','c','c','c','c','c','c','c',
              'c','c','c','c','c','c','c','c','c','c','c','c',
              'c','c','c','c','c','c','c','c','c','c','c','c',
              'c','c','c','c','c','c','c','c','c','c','c','c',
              'c','c','c','c','c','c','c','c','c','c','c','c',
              'c','c','c','c','c','c','c','c','c','c','c','c',
              'c','c','c','c','c','c','c','c','c','c','c','c',
              'c','c','c','c','c','c','c','c','c','c','c','c'
      };
        int i = new Solution().compress(chars);
        for (int j = 0; j < i; j++) {
            System.out.print(chars[j]+" ");
        }

    }
}
class Solution {
    public int compress(char[] chars) {
        int curi=0;
        int index=0;
        while(curi<chars.length){
            char cur=chars[curi];
            int count=0;
            while(curi<chars.length&&chars[curi]==cur){
                count++;
                curi++;
            }
            chars[index++]=cur;
            if (count>1){
                for(char c:String.valueOf(count).toCharArray()){
                    chars[index++]=c;
                }
            }

        }
        return index;
    }
}

//class Solution {
//    public int compress(char[] chars) {
//        StringBuilder sb=new StringBuilder();
//        Map<Character,Integer> map=new HashMap<>();
//        for(int i=0;i<chars.length;i++){
//            if(!map.containsKey(chars[i])){
//                map.put(chars[i],1);
//            }else{
//                int num=map.get(chars[i]);
//                map.put(chars[i],num+1);
//            }
//        }
//        for(Map.Entry<Character,Integer> entry:map.entrySet()){
//            sb.append(entry.getKey());
//            if(entry.getValue()!=1){
//                char[] charArray = entry.getValue().toString().toCharArray();
//                if (charArray.length>1){
//                    for (char arr:charArray){
//                        sb.append(arr);
//                    }
//                }else {
//                    sb.append(entry.getValue());
//                }
//            }
//        }
//
//        char[] newchar=sb.toString().toCharArray();
//        for (int i = 0; i < chars.length; i++) {
//            if (i<newchar.length){
//                chars[i]=newchar[i];
//            }
//        }
//        return chars.length;
//    }
//}
